package xcmg.device.web.agent;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.AgentAllotService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.agent.AgentAllotAddVO;
import xcmg.device.service.vo.agent.AgentAllotVO;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.Ref;
import yb.ecp.fast.infra.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/agentAllot")
public class AgentAllotController extends BasicController {
    @Autowired
    private AgentAllotService agentAllotService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "增加代理商间调拨记录")
    public ActionResult warehouse(@RequestBody AgentAllotAddVO agentAllotAddVO)throws Exception{
        if(null == agentAllotAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        Ref<List<String>> materialNos = new Ref<>(new ArrayList<>());
        ErrorCode checkCode = agentAllotService.checkCondition(agentAllotAddVO,materialNos);
        if(ErrorCode.Success!=checkCode)
        {
            return actionResult(checkCode,materialNos.get());
        }

        agentAllotAddVO.getHeaderInfo().setVerifyState(0);
        if(agentAllotAddVO.getHeaderInfo().getId()==null|| StringUtil.isNullOrEmpty(agentAllotAddVO.getHeaderInfo().getId())){
            ErrorCode errorCode = agentAllotService.add(agentAllotAddVO, getUserId(), getOrgId());
            return actionResult(errorCode);
        }else {
            ErrorCode errorCode = agentAllotService.update(agentAllotAddVO, getOrgId());
            return actionResult(errorCode);
        }
    }
    @RequestMapping(value = "/submit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "增加代理商间调拨记录")
    public ActionResult sumbit(@RequestBody AgentAllotAddVO agentAllotAddVO)throws Exception{
        if(null == agentAllotAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        agentAllotAddVO.getHeaderInfo().setVerifyState(1);
        if(agentAllotAddVO.getHeaderInfo().getId()==null|| StringUtil.isNullOrEmpty(agentAllotAddVO.getHeaderInfo().getId())){
            ErrorCode errorCode = agentAllotService.add(agentAllotAddVO, getUserId(), getOrgId());
            return actionResult(errorCode);
        }else {
            ErrorCode errorCode = agentAllotService.update(agentAllotAddVO, getOrgId());
            return actionResult(errorCode);
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新代理商间调拨记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody AgentAllotAddVO agentAllotAddVO)throws Exception{
        if(null == agentAllotAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = agentAllotService.update(agentAllotAddVO, getOrgId());
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/updateState",method = RequestMethod.POST)
    @ApiOperation(value = "更新代理商间调拨状态")
    @FastMappingInfo(needLogin = true,code = AuthCode.ClientAllocation.AUDIT)
    public ActionResult updateState(@RequestBody AgentAllotVO agentAllotVO)throws Exception{
        if(null == agentAllotVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode = agentAllotService.updateSate(agentAllotVO, getUserId());
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("代理商间调拨记录删除")
    @FastMappingInfo(needLogin = true,code = AuthCode.ClientAllocation.DEL)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(agentAllotService.remove(Arrays.asList(ids)));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id)throws Exception{
        return actionValueResult(agentAllotService.findByPK(id, "id", getOrgId()));
    }

    @RequestMapping(value = "/itemByNo",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByNo(String allotOrder)throws Exception{
        return actionValueResult(agentAllotService.findByPK(allotOrder, "allotOrder", getOrgId()));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询代理商间调拨记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<AgentAllotVO> condition)throws Exception{
        AgentAllotVO agentAllotVO =condition.getFilters();
        if(agentAllotVO == null){
            agentAllotVO = new AgentAllotVO();
        }
        agentAllotVO.setAllOrgId(getOrgId());
        condition.setFilters(agentAllotVO);
        PageCommonVO pageCommonVO= agentAllotService.list(condition, getOrgId());
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出代理商间调拨记录列表")
    @FastMappingInfo(needLogin = true,code = AuthCode.ClientAllocation.EXPORT)
    public void export(String[] ids)throws Exception {
        agentAllotService.export(Arrays.asList(ids),getOrgId());
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部代理商间调拨记录列表")
    @FastMappingInfo(needLogin = true,code = AuthCode.ClientAllocation.EXPORT)
    public void exportAll(AgentAllotVO condition)throws Exception {
        if(condition == null){
            condition = new AgentAllotVO();
        }
        condition.setAllOrgId(getOrgId());
        agentAllotService.exportAll(condition,getOrgId());
    }

    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("零件批量导入")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        String orgId = request.getParameter("x-orgId");
        if(StringUtil.isNullOrSpace(orgId))
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {

            InputStream in = file.getInputStream();
            result= agentAllotService.importExcel(in,  orgId);
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("下载零件批量导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        agentAllotService.download();
    }

    @RequestMapping(value = "/manualFinishIn", method = RequestMethod.POST)
    @ApiOperation("强制完成单据")
    public ActionResult manualFinishIn(@RequestBody ManualFinishVO manualFinishVO) throws Exception
    {
        return actionResult(agentAllotService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId(),0));
    }


    @RequestMapping(value = "/manualFinishOut", method = RequestMethod.POST)
    @ApiOperation("强制完成单据")
    public ActionResult manualFinishOut(@RequestBody ManualFinishVO manualFinishVO) throws Exception
    {
        return actionResult(agentAllotService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId(),1));
    }


    @RequestMapping(value = "/settle", method = RequestMethod.GET)
    @ApiOperation("结算")
    @FastMappingInfo(needLogin = true)
    public ActionResult settle(@RequestParam(value = "id") String id,@RequestParam(value = "remark",required = false) String remark) {

        AgentAllotVO agentAllotVO = new AgentAllotVO();
        agentAllotVO.setId(id);
        agentAllotVO.setSettelId(getUserId());
        agentAllotVO.setSettleName(getUserName());
        agentAllotVO.setRemark(remark);
        return actionValueResult(agentAllotService.settle(agentAllotVO));
    }

}
